package org.wxd.springcloud.mdc.config;

import lombok.extern.slf4j.Slf4j;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;
import org.springframework.ui.ModelMap;
import org.springframework.web.context.request.WebRequest;
import org.springframework.web.context.request.WebRequestInterceptor;

import java.util.UUID;


@Slf4j
@Component
public class LogInterceptor implements WebRequestInterceptor {


    @Override
    public void preHandle(WebRequest webRequest) {
        log.info("preHandle.......before traceId");
        MDC.put("traceId", UUID.randomUUID().toString().replace("-", ""));
        log.info("preHandle.......");
    }

    @Override
    public void postHandle(WebRequest webRequest, ModelMap modelMap) {

    }

    @Override
    public void afterCompletion(WebRequest webRequest, Exception e) {
        log.info("afterCompletion.......");
        MDC.remove("traceId");
        log.info("afterCompletion.......after remove");
    }
}
